#!/bin/bash

exec 2>/dev/null

detect=1
x=0
y=0
size=100
max_detect=20

show_help()
{
        echo "$0 x y side_length"
        echo "example: Create a touch square at (462,250) with side length 100"
        echo -e "\t$0 462 250 100"
}

check_Touch()
{
        if [ $x -ge $x_limit ] &&\
         [ $y -ge $y_limit ] &&\
         [ $x -le $(( x_limit+size )) ] &&\
         [ $y -le $(( y_limit+size )) ]; then                     
                echo "($x, $y)"
                echo "TsTest:PASS"
        else                                                                    
                echo "($x, $y)"
                echo "TsTest:FAIL"
        fi                                            
}


if [ $# -eq 3 ]; then           
        x_limit=$1
        y_limit=$2
        size=$3
else              
        show_help
        exit 1
fi                 

modprobe evbug

if [ -s touchlog ]
then
        rm touchlog
fi

/home/root/jabil/drawrectEx $x_limit $y_limit $size &                          
                                                                               
# Get touch even from kmsg                                                     
while [ $detect -le $max_detect ]                                              
do                                                                             
        cat /proc/kmsg > touchlog &                                            
        sleep 0.5                                                              
        #kill -9 $(jobs -p)                                                    
        kill -9 $(ps| grep "[c]at /proc/kmsg" | awk '{print $1}')              
                                                                               
        if [ -s touchlog ]                                                     
        then                                                     
                detect=99                                        
        else                                                     
                (( detect++ ))                                   
        fi                                                       
done                                                             
                                                                 
kill -2 $(ps| grep "[/]home/root/jabil/drawrectEx" | awk '{print $1}') 

# parse coordinates
if [ -s touchlog ]; then
        x=$(grep 'Code: 53, Value:' touchlog | head -n 1 | awk '{print $10}')
        y=$(grep 'Code: 54, Value:' touchlog | head -n 1 | awk '{print $10}')

        check_Touch
        rm touchlog
else
        echo "Timeout"
        echo "TsTest:FAIL"
fi
modprobe -r evbug
